package com.lti.appnet.module.event;

import com.google.common.collect.Lists;
import com.lti.appnet.module.domain.Category;
import com.lti.appnet.module.domain.Module;
import com.lti.appnet.module.repository.ModuleRepository;
import com.lti.appnet.module.response.CategoryRes;
import com.lti.appnet.module.response.ModuleRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sinceow on 2014/7/30.
 */
@Service
public class ModuleReadEvent {

    @Autowired
    ModuleRepository moduleRepository;

    @Autowired
    CategoryReadEvent categoryReadEvent;

    // return system module + advisor module
    public List<ModuleRes> getAuthorAllModules(String authorId) {
        List<Module> modules = moduleRepository.findAllIncludeAuthorId(authorId);
        List<ModuleRes> moduleReses = Lists.newArrayList();
        for(Module module: modules){
            ModuleRes moduleRes = new ModuleRes(module);

            List<Category> categories  = categoryReadEvent.getCategoriesByModuleAndAuthor(module.getID(), module.getAuthorId());
            List<CategoryRes> categoryReses = Lists.newArrayList();
            for(Category category: categories){
                categoryReses.add(new CategoryRes(category));
            }
            moduleRes.setCategories(categoryReses);
            moduleReses.add(moduleRes);
        }

        return moduleReses;
    }

    public List<Module> getAuthorModules(String authorId) {
        return moduleRepository.findByAuthorId(authorId);
    }

}
